package com.cyy.utils;

import org.openqa.selenium.By;

public class ElementBy {

    /**
     * 根据字符串解析by对象，字符串的格式 name=value
     * @param sby 要解析的by对象
     * @return
     */
    public static By getBy(String sby) {
        String value="";
        String target = sby.replace(" ","").toLowerCase();

        if (target.startsWith("id=")) {
            value = sby.substring(3).trim();
            return By.id(value);
        }

        if (target.startsWith("xpath=")) {
            value = sby.substring(6).trim();
            return By.xpath(value);
        }

        if (target.startsWith("classname=")) {
            value = sby.substring(10).trim();
            return By.className(value);
        }

        if (target.startsWith("name=")) {
            value = sby.substring(5).trim();
            return By.name(value);
        }


        if (target.startsWith("cssselector=")) {
            value = sby.substring(12).trim();
            return By.cssSelector(value);
        }

        return null;
    }


    public static void main(String[] args) {
        String sby = "id=//*[@id='com.cyyserver:id/sign_name'][text()='客户']";
        System.out.println(getBy(sby));
    }

}
